package com.yeshine.opendata.controller.front.my.org;

import com.yeshine.opendata.controller.front.AbstractFrontControllerBase;
import com.yeshine.opendata.entity.*;
import com.yeshine.opendata.service.IMemberService;
import com.yeshine.opendata.service.IRoleService;
import com.yeshine.opendata.service.IUserService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by taohj on 2014/9/3.
 */
@Controller
@RequestMapping("/front/my/org/member")
public class MyMemberController extends AbstractFrontControllerBase {

    @Autowired
    private IMemberService service;

    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService roleService;

    @RequestMapping("createV")
    public String createV(
            @RequestParam(value = "page",required = false) Integer page,
            @RequestParam(value = "size",required = false) Integer size,
            @RequestParam(value = "id",required = false) Integer id,
            @RequestParam(value = "username",required = false) String username,
            @RequestParam(value = "sex",required = false) Integer sex,
            @RequestParam("org") Integer org
    ) {
        User user=new User();
        user.setId(id);
        user.setUsername(username);
        user.setSex(new SysDictionary(sex));

        PageResult<User> pageResult=new PageResult<User>(page,size);
        pageResult.setT(user);
        pageResult.setWhere(" and user.id  not in ( select user from T_Member where org='"+org+"' ) ");
        Result<User> result=userService.getByPage(pageResult);
        setRequestAttr("result", result);
        setRequestAttr("org", org);
        return "/front/my/org/member/create";
    }




    @ResponseBody
    @RequestMapping("create")
    public String create(
            @RequestParam("org") Integer org,
            @RequestParam("users[]") Integer users[],
            @RequestParam(value = "role",required = false) Integer role
    ) {
        List<Member> members=new ArrayList<Member>();
        if (users!=null && users.length>0){
            for(Integer user:users){
                Member member=new Member();
                members.add(member);
                member.setUser(new User(user));
                member.setOrg(new Org(org));
                member.setRole(new Role(role));
            }
        }
        Result<Member> result=service.create(members);
        return result.toString();

    }



    @RequestMapping("modifyV")
    public String modifyV(
           @RequestParam("id") Integer id
    ) {
        Member member=service.getById(id).getTarget();
        Role role=new Role();
        role.setOrg(member.getOrg());
        List<Role> roles=roleService.getList(role).getList();
        setRequestAttr("member",member);
        setRequestAttr("roles",roles);
        return "/front/my/org/member/specifyRoleList";
    }





    @RequestMapping("modify")
    public String modify(
            @RequestParam(value = "id") Integer id,
            @RequestParam(value = "role") Integer role
    ) {

        Member member=service.getById(id).getTarget();
        member.setRole(new Role(role));
        Result<Member> result=service.modify(member);
        return  getReturnString(result,"redirect:/front/my/org/member/list?org="+member.getOrg().getId(),setMessage("操作失败","修改成员角色失败"));
    }

    @ResponseBody
    @RequestMapping("remove")
    public  String remove(
            @RequestParam("ids[]") Integer ids[]
    ){
        List<Member> members=new ArrayList<Member>();
        if (ids!=null && ids.length>0){
            for(Integer id:ids){
                Member member=new Member(id);
                members.add(member);

            }
        }
        Result<Member> result=service.remove(members);
        return result.toString();
    }


    @RequestMapping("list")
    public  String list(
            @RequestParam(value = "page",required = false) Integer page,
            @RequestParam(value = "size",required = false) Integer size,
            @RequestParam("org") Integer org
    ){

        Member member=new Member();
        member.setOrg(new Org(org));
        PageResult<Member> pageResult=new PageResult<Member>(page,size);
        pageResult.setT(member);
        Result<Member> result=service.getByPage(pageResult);
        setRequestAttr("result",result);
       return  "front/my/org/member/list";
    }

}
